
package com.tools.ad.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

import com.tools.ad.R;
import com.tools.ad.util.FontsManager;


public class FontTextView extends TextView {

    private int mFontType = -1;
    private int mFontSize1 = 50;
    private int mFontSize2 = 70;
    private boolean mAutoResize = false;
    private boolean mIsUseFontEver = false;

    private TextPaint mTextPaint;

    public FontTextView(Context context) {
        this(context, null, 0);
    }

    public FontTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        readAttrs(context, attrs);
    }

    private void readAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        int fontType = FontsManager.FONT_LIGHT;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.FontTextView_fontType) {
                fontType = a.getInteger(attr, FontsManager.FONT_LIGHT);
            } else if (attr == R.styleable.FontTextView_autoResize) {
                mAutoResize = a.getBoolean(attr, mAutoResize);
            } else if (attr == R.styleable.FontTextView_fontSize1) {
                mFontSize1 = a.getDimensionPixelSize(attr, mFontSize1);
            } else if (attr == R.styleable.FontTextView_fontSize2) {
                mFontSize2 = a.getDimensionPixelSize(attr, mFontSize2);
            } else if (attr == R.styleable.FontTextView_isUseFontEver) {
                mIsUseFontEver = a.getBoolean(attr, false);
            }
        }
        a.recycle();

        setFontType(fontType);
    }

    /**
     * 设置字体类别，THIN 用于大圈内部的数字，LIGHT 用于正文英文字体
     *
     * @return {@link FontsManager#FONT_THIN} 或 {@link FontsManager#FONT_LIGHT}
     */
    public void setFontType(int type) {
        if (type == mFontType) {
            return;
        }

        if (!mIsUseFontEver && !FontsManager.isEnglish(getContext())) {
            return;
        }

        Typeface tf = FontsManager.getFontType(type);
        if (tf != null) {
            //only for En Special FontType
            setIncludeFontPadding(false);
            setTypeface(tf);
        }
    }

    /**
     * 获取字体类别
     *
     * @return {@link FontsManager#FONT_THIN} 或 {@link FontsManager#FONT_LIGHT}
     */
    public int getFontType() {
        return mFontType;
    }

    private void fitWidth(String text, int textWidth) {
        final int desiredWidth = textWidth - getPaddingLeft() - getPaddingRight();
        if (desiredWidth <= 0) {
            return;
        }
        int textSize = mFontSize1;
        if (mTextPaint == null) {
            mTextPaint = new TextPaint();
        }
        mTextPaint.set(getPaint());
        mTextPaint.setTextSize(textSize);

        if (mTextPaint.measureText(text) >= desiredWidth) {
            textSize = mFontSize2;
        }

        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);

        if (mAutoResize && length() > 0) {
            fitWidth(text.toString(), getWidth());
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw && mAutoResize && length() > 0) {
            fitWidth(getText().toString(), w);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mAutoResize && length() > 0) {
            fitWidth(getText().toString(), getMeasuredWidth());
        }
    }

}
